#include "web_interface.h"
#include "websocket_helper.hpp"
WebInterface::WebInterface(WebBehaviorConfig *config, int threadNum)
{
  if (config && config->key_file_name && config->cert_file_name)
  {
    WebServer<true> *server = new WebServer<true>();
    server->initConfig(config, threadNum);
    m_handle = (void *)server;
    m_bSSL = true;
  }
  else
  {
    m_bSSL = false;
    WebServer<false> *server = new WebServer<false>();
    server->initConfig(config, threadNum);
    m_handle = (void *)server;
  }
}

WebInterface::~WebInterface()
{
  if (!m_handle)
  {
    return;
  }
  if (m_bSSL)
  {
    WebServer<true> *server = (WebServer<true> *)m_handle;
    delete server;
    server = nullptr;
  }
  else
  {
    WebServer<false> *server = (WebServer<false> *)m_handle;
    delete server;
    server = nullptr;
  }
}

void WebInterface::initHttp(HttpMethod method, const std::string path, std::function<void(std::shared_ptr<HttpData>)> func)
{
  if (!m_bSSL)
  {
    WebServer<false> *server = (WebServer<false> *)m_handle;
    server->registerHttp2(method, path, func);
  }
  else
  {
    WebServer<true> *server = (WebServer<true> *)m_handle;
    server->registerHttp2(method, path, func);
  }
}

void WebInterface::initWs(WSMethod method, const std::string path, std::function<void(std::shared_ptr<WsData>)> func)
{
  if (!m_bSSL)
  {
    WebServer<false> *server = (WebServer<false> *)m_handle;
    server->registerWS2(method, path, func);
  }
  else
  {
    WebServer<true> *server = (WebServer<true> *)m_handle;
    server->registerWS2(method, path, func);
  }
}

void WebInterface::start(int port)
{
  if (!m_bSSL)
  {
    WebServer<false> *server = (WebServer<false> *)m_handle;
    server->start(port);
  }
  else
  {
    WebServer<true> *server = (WebServer<true> *)m_handle;
    server->start(port);
  }
}